$().ready(function(e) {
    $(".reset-code").click(function(){
		$("img.reset-code").attr('src', img_verify_code_url + '&' + Math.floor(Math.random()*9999));
	});
	$('.datepicker').datepicker({
		dateFormat: 'yy-m-d',
		showOn: 'both', 
		buttonImage: '/Public/front/images/calendar.gif',
	buttonImageOnly: true});
	
	$(".js-confirm").click(function(){
		if (confirm("您确定要继续操作吗？")) {
			return true;
		} else {
			return false;
		}
	});
	
	$(".btn-submit").click(function(){
		var $form   = $(this).parents('form');
		if ($(this).attr('data-action') && $(this).attr('data-action').length>0) {
			action  = $(this).attr('data-action');
		} else {
			action  = $form.attr('action');
		}
		$.post(action, $form.serialize(),function(json){
				resDo(json);
			},"json");
	});
	
	$("#province_id").change(function(){ loadArea('city_id',$(this).val(),0); });
	$("#city_id").change(function(){ loadArea('district_id',$(this).val(),0); });
	$("#district_id").change(function(){ loadArea('street_id',$(this).val(),0); });
	
	$(".pop-up").click(function(){
		var title = $(this).attr('data-title');
		var url	  = $(this).attr('data-url');
		var h	  = $(this).attr('data-h');
		var w 	  = $(this).attr('data-w');
		if (typeof(h)=="undefined") h=460;
		if (typeof(w)=="undefined") w=640;
		tb_show(title, url + '&height='+h+'&width='+w+'&modal=false&TB_iframe', false);
	});
	
	$(".ico_cut").click(function(){
		buy_num = parseInt($(".buy_num").val());
		if (buy_num>1) buy_num--;
		$(".buy_num").val(buy_num);
	});
	
	$(".ico_plus").click(function(){
		buy_num = parseInt($(".buy_num").val());
		buy_num++;
		$(".buy_num").val(buy_num);
	});	
	
	$(".goods-spec-list i").click(function(){
		$(this).siblings().removeClass('cur');
		$(this).addClass('cur');
	});
});

function loadArea(obj_name,id,selected) {
	$.post(ajax_load_area_url,{'id':id},function(json){
			$obj = $("#"+obj_name);
			$obj.empty();
			$.each(json, function(i,v){
				if (v.id==selected) {
					sel = "selected";
				} else {
					sel = "";
				}
				$obj.append('<option value="'+v.id+'" '+sel+'>'+v.name+'</option>');
			});
		if(obj_name=='city_id' && selected==0){//修改的时候需要判断
			$.post(ajax_load_area_url,{'id':json[0].id},function(json){
				$obj2 = $("#district_id");
				$obj2.empty();
				$.each(json, function(i,v){
					if (v.id==selected) {
						sel = "selected";
					} else {
						sel = "";
					}
					$obj2.append('<option value="'+v.id+'" '+sel+'>'+v.name+'</option>');
				});

			},"json");
		}
		
		},"json");
}
//通用关联数据使用
function loadArea_new(obj_name,id,selected,$tanleName) {
	$.ajaxSettings.async = false;
	var ids=id; 
	$.each(obj_name,function(index,value){
		$.post(linkTableName,{'id':ids,'tname':$tanleName},function(json){
			$obj = $("#"+value);
			$obj.empty();
			ids=json[0].id;
			$.each(json, function(i,v){
				if (v.id==selected) {
					sel = "selected";
				} else {
					sel = "";
				}
				$obj.append('<option value="'+v.id+'" '+sel+'>'+v.name+'</option>');
			});
		},"json");
	})
	$.ajaxSettings.async = true;
}




function init_pc($pid,$cid) {
	if ($pid=='' || $pid=='0') $pid = $("#province_id").val();
	load_city($pid,$cid);
	$("#province_id").change(function(){
		load_city($(this).val(),0);
	});
}

function load_city($pid,$cid) {
      $.ajax({
        url:load_city_url,
        type:"POST",
        dataType:"json",
        data:{'pid':$pid},
        timeout:6000,
        success:function(json){
			$city = $("#city_id");
			$city.empty();
			$.each(json, function(i,v){
				if (v.id==$cid) {
					sel = "selected";
				} else {
					sel = "";
				}
				$city.append('<option value="'+v.id+'" '+sel+'>'+v.name+'</option');
			});
        },
        error:function() {
          alert('网络错误,请重试');
        }
      });	
}


// ######弹窗#########

var tb_pathToImage = "Public/front/images/loadingAnimation.gif"; /*thickbox*/
$(document).ready(function() {
	tb_init("a.thickbox, area.thickbox, input.thickbox");
	imgLoader = new Image();
	imgLoader.src = tb_pathToImage
});

function tb_init(a) {
	$(a).click(function() {
		var c = this.title || this.name || null;
		var b = this.href || this.alt;
		var d = this.rel || false;
		tb_show(c, b, d);
		this.blur();
		return false
	})
}
function tb_show(j, b, h) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body", "html").css({
				height: "100%",
				width: "100%"
			});
			$("html").css("overflow", "hidden");
			if (document.getElementById("TB_HideSelect") === null) {
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove)
			}
		} else {
			if (document.getElementById("TB_overlay") === null) {
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove)
			}
		}
		if (tb_detectMacXFF()) {
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack")
		} else {
			$("#TB_overlay").addClass("TB_overlayBG")
		}
		if (j === null) {
			j = ""
		}
		$("body").append("<div id='TB_load'><img src='" + imgLoader.src + "' /></div>");
		$("#TB_load").show();
		var c;
		if (b.indexOf("?") !== -1) {
			c = b.substr(0, b.indexOf("?"))
		} else {
			c = b
		}
		var f = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
		var k = c.toLowerCase().match(f);
		if (k == ".jpg" || k == ".jpeg" || k == ".png" || k == ".gif" || k == ".bmp") {
			TB_PrevCaption = "";
			TB_PrevURL = "";
			TB_PrevHTML = "";
			TB_NextCaption = "";
			TB_NextURL = "";
			TB_NextHTML = "";
			TB_imageCount = "";
			TB_FoundURL = false;
			if (h) {
				TB_TempArray = $("a[@rel=" + h + "]").get();
				for (TB_Counter = 0;
				((TB_Counter < TB_TempArray.length) && (TB_NextHTML === "")); TB_Counter++) {
					var g = TB_TempArray[TB_Counter].href.toLowerCase().match(f);
					if (!(TB_TempArray[TB_Counter].href == b)) {
						if (TB_FoundURL) {
							TB_NextCaption = TB_TempArray[TB_Counter].title;
							TB_NextURL = TB_TempArray[TB_Counter].href;
							TB_NextHTML = "<span id='TB_next'>&nbsp;&nbsp;<a href='#'>Next &gt;</a></span>"
						} else {
							TB_PrevCaption = TB_TempArray[TB_Counter].title;
							TB_PrevURL = TB_TempArray[TB_Counter].href;
							TB_PrevHTML = "<span id='TB_prev'>&nbsp;&nbsp;<a href='#'>&lt; Prev</a></span>"
						}
					} else {
						TB_FoundURL = true;
						TB_imageCount = "Image " + (TB_Counter + 1) + " of " + (TB_TempArray.length)
					}
				}
			}
			imgPreloader = new Image();
			imgPreloader.onload = function() {
				imgPreloader.onload = null;
				var n = tb_getPageSize();
				var l = n[0] - 150;
				var q = n[1] - 150;
				var m = imgPreloader.width;
				var e = imgPreloader.height;
				if (m > l) {
					e = e * (l / m);
					m = l;
					if (e > q) {
						m = m * (q / e);
						e = q
					}
				} else {
					if (e > q) {
						m = m * (q / e);
						e = q;
						if (m > l) {
							e = e * (l / m);
							m = l
						}
					}
				}
				TB_WIDTH = m + 30;
				TB_HEIGHT = e + 60;
				$("#TB_window").append("<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='" + b + "' width='" + m + "' height='" + e + "' alt='" + j + "'/></a><div id='TB_caption'>" + j + "<div id='TB_secondLine'>" + TB_imageCount + TB_PrevHTML + TB_NextHTML + "</div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a> or Esc Key</div>");
				$("#TB_closeWindowButton").click(tb_remove);
				if (!(TB_PrevHTML === "")) {
					function p() {
						if ($(document).unbind("click", p)) {
							$(document).unbind("click", p)
						}
						$("#TB_window").remove();
						$("body").append("<div id='TB_window'></div>");
						tb_show(TB_PrevCaption, TB_PrevURL, h);
						return false
					}
					$("#TB_prev").click(p)
				}
				if (!(TB_NextHTML === "")) {
					function o() {
						$("#TB_window").remove();
						$("body").append("<div id='TB_window'></div>");
						tb_show(TB_NextCaption, TB_NextURL, h);
						return false
					}
					$("#TB_next").click(o)
				}
				document.onkeydown = function(r) {
					if (r == null) {
						keycode = event.keyCode
					} else {
						keycode = r.which
					}
					if (keycode == 27) {
						tb_remove()
					} else {
						if (keycode == 190) {
							if (!(TB_NextHTML == "")) {
								document.onkeydown = "";
								o()
							}
						} else {
							if (keycode == 188) {
								if (!(TB_PrevHTML == "")) {
									document.onkeydown = "";
									p()
								}
							}
						}
					}
				};
				tb_position();
				$("#TB_load").remove();
				$("#TB_ImageOff").click(tb_remove);
				$("#TB_window").css({
					display: "block"
				})
			};
			imgPreloader.src = b
		} else {
			var a = b.replace(/^[^\?]+\??/, "");
			var d = tb_parseQuery(a);
			TB_WIDTH = (d.width * 1) + 30 || 630;
			TB_HEIGHT = (d.height * 1) + 40 || 440;
			ajaxContentW = TB_WIDTH - 30;
			ajaxContentH = TB_HEIGHT - 45;
			if (b.indexOf("TB_iframe") != -1) {
				urlNoQuery = b.split("TB_");
				$("#TB_iframeContent").remove();
				if (d.modal != "true") {
					$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + j + "</div><div id='TB_closeAjaxWindow'><a href='javascript:;' id='TB_closeWindowButton' title='关闭'>关闭</a></div></div><iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent" + Math.round(Math.random() * 1000) + "' onload='tb_showIframe()' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;' > </iframe>")
				} else {
					$("#TB_overlay").unbind();
					$("#TB_window").append("<iframe frameborder='0' hspace='0' src='" + urlNoQuery[0] + "' id='TB_iframeContent' name='TB_iframeContent" + Math.round(Math.random() * 1000) + "' onload='tb_showIframe()' style='width:" + (ajaxContentW + 29) + "px;height:" + (ajaxContentH + 17) + "px;'> </iframe>")
				}
			} else {
				if ($("#TB_window").css("display") != "block") {
					if (d.modal != "true") {
						$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>" + j + "</div><div id='TB_closeAjaxWindow'><a href='javascript:;' id='TB_closeWindowButton'>关闭</a></div></div><div id='TB_ajaxContent' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px'></div>")
					} else {
						$("#TB_overlay").unbind();
						$("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:" + ajaxContentW + "px;height:" + ajaxContentH + "px;'></div>")
					}
				} else {
					$("#TB_ajaxContent")[0].style.width = ajaxContentW + "px";
					$("#TB_ajaxContent")[0].style.height = ajaxContentH + "px";
					$("#TB_ajaxContent")[0].scrollTop = 0;
					$("#TB_ajaxWindowTitle").html(j)
				}
			}
			$("#TB_closeWindowButton").click(tb_remove);
			if (b.indexOf("TB_inline") != -1) {
				$("#TB_ajaxContent").append($("#" + d.inlineId).children());
				$("#TB_window").unload(function() {
					$("#" + d.inlineId).append($("#TB_ajaxContent").children())
				});
				tb_position();
				$("#TB_load").remove();
				$("#TB_window").css({
					display: "block"
				})
			} else {
				if (b.indexOf("TB_iframe") != -1) {
					tb_position();
					if ($.browser.safari) {
						$("#TB_load").remove();
						$("#TB_window").css({
							display: "block"
						})
					}
				} else {
					$("#TB_ajaxContent").load(b += "&random=" + (new Date().getTime()), function() {
						tb_position();
						$("#TB_load").remove();
						tb_init("#TB_ajaxContent a.thickbox");
						$("#TB_window").css({
							display: "block"
						})
					})
				}
			}
		}
		if (!d.modal) {
			document.onkeyup = function(l) {
				if (l == null) {
					keycode = event.keyCode
				} else {
					keycode = l.which
				}
				if (keycode == 27) {
					tb_remove()
				}
			}
		}
	} catch (i) {}

}
function tb_showIframe() {
	$("#TB_load").remove();
	$("#TB_window").css({
		display: "block"
	})
}
function tb_remove() {
	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast", function() {
		$("#TB_window,#TB_overlay,#TB_HideSelect").trigger("unload").unbind().remove()
	});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {
		$("body", "html").css({
			height: "auto",
			width: "auto"
		});
		$("html").css("overflow", "")
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false
}
function tb_position() {
	$("#TB_window").css({
		marginLeft: "-" + parseInt((TB_WIDTH / 2), 10) + "px",
		width: TB_WIDTH + "px"
	});
	if (!(jQuery.browser.msie && jQuery.browser.version < 7)) {
		$("#TB_window").css({
			marginTop: "-" + parseInt((TB_HEIGHT / 2), 10) + "px"
		})
	}
}
function tb_parseQuery(d) {
	var e = {};
	if (!d) {
		return e
	}
	var a = d.split(/[;&]/);
	for (var c = 0; c < a.length; c++) {
		var g = a[c].split("=");
		if (!g || g.length != 2) {
			continue
		}
		var b = unescape(g[0]);
		var f = unescape(g[1]);
		f = f.replace(/\+/g, " ");
		e[b] = f
	}
	return e
}
function tb_getPageSize() {
	var c = document.documentElement;
	var a = window.innerWidth || self.innerWidth || (c && c.clientWidth) || document.body.clientWidth;
	var b = window.innerHeight || self.innerHeight || (c && c.clientHeight) || document.body.clientHeight;
	arrayPageSize = [a, b];
	return arrayPageSize
}
function tb_detectMacXFF() {
	var a = navigator.userAgent.toLowerCase();
	if (a.indexOf("mac") != -1 && a.indexOf("firefox") != -1) {
		return true
	}
};

